package com.ly.sy.controller;


import com.ly.sy.entity.Syadmin;
import com.ly.sy.entity.Syuser;
import com.ly.sy.service.SyadminService;
import com.ly.sy.util.saveObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2020-11-11
 */
@Controller
@RequestMapping("/sy/syadmin")
public class SyadminController {

    @Autowired
    private SyadminService syadminService;

//    跳转到后台登录页面
    @GetMapping("admin")
    public String goadmin(){
        return "/admin/adminlogin";
    }
    //欢迎页面
    @GetMapping("welcome")
    public String welcome(){
        System.out.println("==========================欢迎来到管理员后台========================");
        return "/admin/welcome";
    }
    //刷新
    @GetMapping("index")
    public String index(){
        return "/admin/index";
    }
    //管理员登录
    @PostMapping("adminlogin")
    public String adminlogin(String username,String password, HttpSession session){


            if(username == "" || password == ""){
                session.setAttribute("adminmessage","1");
                return "admin/adminlogin";
            }
            Syadmin syadmin = syadminService.findAdminByUnAndpwd(username,password);

            if(ObjectUtils.isEmpty(syadmin)){
                session.setAttribute("adminmessage","2");
                return "admin/adminlogin";
            }

            session.setAttribute("admin",syadmin);
            return "/admin/index";
    }
    //admin logout and remove all session
    @GetMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("admin");
        return "admin/adminlogin";
    }
    //图片上传
    @PostMapping("uploadFile")
    @ResponseBody
    public String uploadFile(
            @RequestParam("imageData") MultipartFile file,
            HttpSession session
    ){
        if(ObjectUtils.isEmpty(file)){
            System.out.println("没有获取到文件");
            return "null";
        }
        System.out.println("获取到的上传文件："+file);
//      上传图片到oss，并返回一个图片路径url
        String url = saveObject.updown(file);

        System.out.println("图片上传操作完成");
        System.out.println("图片路径："+url);

        return url;
    }
}

